package com.carpedil.shirodemo.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carpedil.shirodemo.common.util.ResultVO;
import com.carpedil.shirodemo.system.model.Role;
import com.carpedil.shirodemo.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 前端控制器
 *
 * @author carpedil
 * @since 2021-01-05
 */
@Controller
@RequestMapping("/role")
public class RoleController {
  @Autowired private IRoleService roleService;

  @GetMapping
  public String toAdd() {
    return "role/role-add";
  }

  @GetMapping("/{id}")
  public String toEdit(@PathVariable("id") Integer id, ModelMap modelMap) {
    modelMap.addAttribute("role", roleService.getById(id));
    return "role/role-add";
  }

  @GetMapping("/index")
  public String index() {
    return "role/role-list";
  }

  @PostMapping
  @ResponseBody
  public ResultVO addRole(Role role) {
    roleService.save(role);
    return ResultVO.success();
  }

  @PutMapping
  @ResponseBody
  public ResultVO updateRole(Role role) {
    System.out.println("role = " + role);
    roleService.updateById(role);
    return ResultVO.success();
  }

  @DeleteMapping("/{id}")
  @ResponseBody
  public ResultVO deleteRole(@PathVariable("id") Integer id) {
    roleService.deleteRoleById(id);
    return ResultVO.success();
  }

  /**
   * 分页显示所有角色
   *
   * @param page 当前页码
   * @param limit 每页显示数量
   * @param queryRole 查询条件
   * @return
   */
  @GetMapping("/list")
  @ResponseBody
  public ResultVO getList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "limit", defaultValue = "10") Integer limit,
      Role queryRole) {
    Page<Role> rolePage = roleService.selectAllByQuery(page, limit, queryRole);
    return ResultVO.success(rolePage);
  }

  @GetMapping("/{roleId}/own/menu")
  @ResponseBody
  public ResultVO getRoleOwnMenu(@PathVariable("roleId") Integer roleId) {
    return ResultVO.success(roleService.getMenusByRoleId(roleId));
  }

  @GetMapping("/{roleId}/own/operator")
  @ResponseBody
  public ResultVO getOperatorOwnMenu(@PathVariable("roleId") Integer roleId) {
    Integer[] operatorIds = roleService.getOperatorsByRoleId(roleId);
    for (int i = 0; i < operatorIds.length; i++) {
      operatorIds[i] = operatorIds[i] + 10000;
    }
    return ResultVO.success(operatorIds);
  }

  @PostMapping("/{roleId}/grant/operator")
  @ResponseBody
  public ResultVO grantRoleOperatorOwn(
      @PathVariable("roleId") Integer roleId,
      @RequestParam(value = "operatorIds[]", required = false) Integer[] operatorIds) {
    roleService.grantOperator(roleId, operatorIds);
    return ResultVO.success();
  }

  @PostMapping("/{roleId}/grant/menu")
  @ResponseBody
  public ResultVO grantRoleMenuOwn(
      @PathVariable("roleId") Integer roleId,
      @RequestParam(value = "menuIds[]", required = false) Integer[] menuIds) {
    roleService.grantMenu(roleId, menuIds);
    return ResultVO.success();
  }
}
